//
//  AddressPicker.m
//  青旅O2O
//
//  Created by qlwl on 16/6/7.
//  Copyright © 2016年 qlwl. All rights reserved.
//

#import "AddressPicker.h"
#import "UIView+AddTap.h"

@interface AddressPicker ()<UIPickerViewDelegate,UIPickerViewDataSource>
@property (weak, nonatomic) IBOutlet UIPickerView *pickerView;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topLayout;
@property (weak, nonatomic) IBOutlet UIView *topBgView;
@property (weak, nonatomic) IBOutlet UIActivityIndicatorView *indicator;
@property (weak, nonatomic) IBOutlet UILabel *titLab;
@property (weak, nonatomic) IBOutlet UIView *topHeadView;
@property (weak, nonatomic) IBOutlet UIButton *cancelBtn;
@property (weak, nonatomic) IBOutlet UIButton *sureBtn;

@end

@implementation AddressPicker
@synthesize bgColor = _bgColor;

- (void)setDataSource:(id<PickerViewDataSource>)dataSource
{
    _dataSource = dataSource;
    self.pickerView.dataSource = self;
}
- (void)setDelegate:(id<PickerViewDelegate>)delegate
{
    _delegate = delegate;
    self.pickerView.delegate = self;
}
- (void)setShowsSelectionIndicator:(BOOL)showsSelectionIndicator
{
    _showsSelectionIndicator = showsSelectionIndicator;
    self.pickerView.showsSelectionIndicator = _showsSelectionIndicator;
}
- (NSInteger)numberOfComponents
{
    return self.pickerView.numberOfComponents;
}
- (NSInteger)numberOfRowsInComponent:(NSInteger)component
{
    return [self.pickerView numberOfRowsInComponent:component];
}
- (CGSize)rowSizeForComponent:(NSInteger)component
{
    return [self.pickerView rowSizeForComponent:component];
}
- (nullable UIView *)viewForRow:(NSInteger)row forComponent:(NSInteger)component
{
    return [self.pickerView viewForRow:row forComponent:component];
}

- (void)reloadAllComponents
{
    [self.pickerView reloadAllComponents];
}
- (void)reloadComponent:(NSInteger)component
{
    [self.pickerView reloadComponent:component];
}

- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated
{
    [self.pickerView selectRow:row inComponent:component animated:animated];
}
- (NSInteger)selectedRowInComponent:(NSInteger)component
{
    return [self.pickerView selectedRowInComponent:component];
}
#pragma mark PickerViewDataSource

// returns the number of 'columns' to display.
- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    if ([self.dataSource respondsToSelector:@selector(numberOfComponentsInPickerView:)]) {
        return [self.dataSource numberOfComponentsInPickerView:self];
    }
    return 1;
}

// returns the # of rows in each component..
- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    if ([self.dataSource respondsToSelector:@selector(pickerView:numberOfRowsInComponent:)]) {
        return [self.dataSource pickerView:self numberOfRowsInComponent:component];
    }
    return 0;
}
#pragma mark PickerViewDelegate
- (CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component __TVOS_PROHIBITED
{
    if ([self.delegate respondsToSelector:@selector(pickerView:widthForComponent:)]) {
        return [self.delegate pickerView:self widthForComponent:component];
    }
    return self.pickerView.bounds.size.width/3 - 5;
}
- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component __TVOS_PROHIBITED
{
    if ([self.delegate respondsToSelector:@selector(pickerView:rowHeightForComponent:)]) {
        return [self.delegate pickerView:self widthForComponent:component];
    }
    return 30;
}

- (nullable NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component __TVOS_PROHIBITED
{
    if ([self.delegate respondsToSelector:@selector(pickerView:titleForRow:forComponent:)]) {
        return [self.delegate pickerView:self titleForRow:row forComponent:component];
    }
    return nil;
}
- (nullable NSAttributedString *)pickerView:(UIPickerView *)pickerView attributedTitleForRow:(NSInteger)row forComponent:(NSInteger)component NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED
{
    if ([self.delegate respondsToSelector:@selector(pickerView:attributedTitleForRow:forComponent:)]){
        return [self.delegate pickerView:self attributedTitleForRow:row forComponent:component];
    }
    return nil;
}
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view __TVOS_PROHIBITED
{
    if ([self.delegate respondsToSelector:@selector(pickerView:viewForRow:forComponent:reusingView:)]) {
        return [self.delegate pickerView:self viewForRow:row forComponent:component reusingView:view];
    }
    UILabel* pickerLabel = (UILabel*)view;
    if (!pickerLabel){
        pickerLabel = [[UILabel alloc] init];
        pickerLabel.adjustsFontSizeToFitWidth = YES;
        [pickerLabel setTextAlignment:NSTextAlignmentCenter];
        [pickerLabel setBackgroundColor:[UIColor clearColor]];
        [pickerLabel setFont:[UIFont boldSystemFontOfSize:15]];
    }
    pickerLabel.text=[self pickerView:pickerView titleForRow:row forComponent:component];
    return pickerLabel;
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component __TVOS_PROHIBITED
{
    if ([self.delegate respondsToSelector:@selector(pickerView:didSelectRow:inComponent:)]) {
        [self.delegate pickerView:self didSelectRow:row inComponent:component];
    }
}

#pragma mark btnClick

- (IBAction)cancelBtnClick:(id)sender {
    [self remove];
}
- (IBAction)sureBtnClick:(id)sender {
    if (self.sureBtnClick) {
        self.sureBtnClick();
    }
    [self remove];
}
- (void) setTopHeadColor:(UIColor *)topColor// 顶部横条的颜色设置
{
    self.topHeadView.backgroundColor = topColor;
}
- (void) setTopTitle:(NSString *) title; // 设置顶部横条的标题
{
    self.titLab.text = title;
}
- (void) setTopTitleColor:(UIColor *) titleColor; // 设置标题颜色
{
    self.titLab.textColor = titleColor;
}
- (void) setSureOrCancelBtnColor:(UIColor *) btnColor; // 设置取消确定按钮颜色
{
    [self.cancelBtn setTitleColor:btnColor forState:UIControlStateNormal];
    [self.sureBtn setTitleColor:btnColor forState:UIControlStateNormal];
}

- (UIColor *)bgColor
{
    if (!_bgColor) {
        return [UIColor colorWithRed:0 green:0 blue:0 alpha:0.6];
    }
    return _bgColor;
}
//- (void)setBgColor:(UIColor *)bgColor
//{
//    _bgColor = bgColor;
//    self.backgroundColor = _bgColor;
//}


+(instancetype)addressPickerView
{
    AddressPicker *pickerView = [[[NSBundle mainBundle] loadNibNamed:@"AddressPicker" owner:nil options:nil] lastObject];
    pickerView.bounds = CGRectMake(0, 0, iphoneWidth, 252);
    return  pickerView;
}
- (void)setIsLoding:(BOOL)isLoding
{
    if (_isLoding!=isLoding) {
        _isLoding = isLoding;
        self.pickerView.hidden = _isLoding;
        if (_isLoding) {
            [self.indicator startAnimating];
        }else{
            [self.indicator stopAnimating];
        }
    }
}

- (void)showInView:(UIView *)supView
{
    UIView *view = [[UIView alloc] initWithFrame:supView.bounds];
    [view addGestures:tapPress didClick:^{
        [self remove];
    }];
    view.backgroundColor = self.bgColor;
    view.alpha = 0;
    [view addSubview:self];
    [supView addSubview:view];
    self.frame = CGRectMake(0, view.bounds.size.height, self.bounds.size.width, self.bounds.size.height);
    _isShow = YES;
    [UIView animateWithDuration:0.1 animations:^{
        view.alpha = 1;
    } completion:^(BOOL finished) {
        if (finished) {
            [UIView animateWithDuration:0.3 animations:^{
                self.frame = CGRectMake(0, view.bounds.size.height-self.bounds.size.height, self.bounds.size.width, self.bounds.size.height);
            } completion:^(BOOL finished) {
                
            }];
        }
    }];
}

- (void)remove
{
    _isShow = NO;
    [UIView animateWithDuration:0.3 animations:^{
        self.frame = CGRectMake(0, self.superview.bounds.size.height, self.bounds.size.width, self.bounds.size.height);
    } completion:^(BOOL finished) {
        if (finished) {
            [UIView animateWithDuration:0.1 animations:^{
                self.superview.alpha = 0;
            } completion:^(BOOL finished) {
                [self.superview removeFromSuperview];
                [self removeFromSuperview];
            }];
        }
    }];
}

+(instancetype)showInWindowWithSureClick:(void(^)(void))sureClick
{
    AddressPicker *pickerView = [AddressPicker addressPickerView];
    pickerView.sureBtnClick = sureClick;
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    [pickerView showInView:window];
    return pickerView;
}



@end
